package cn.kshell.druid.spring.boot.filter;

import com.alibaba.druid.util.Utils;

import javax.servlet.*;
import java.io.IOException;

/**
 * 过滤Druid Monitor中的底部广告
 *
 * @author kshell
 */
public class CommonJsFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        chain.doFilter(request, response);
        // 重置缓冲区，响应头不会被重置
        response.resetBuffer();
        String commonJsPath = "support/http/resources/js/common.js";
        String text = Utils.readFromResource(commonJsPath);
        // 替换广告调用代码
        text = text.replace("this.buildFooter();", "");
        response.getWriter().write(text);
    }

    @Override
    public void destroy() {

    }
}
